﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
/*
	Replace calls to misplaced functions in Conninfo with calls internal to the driver.
*/
namespace SqlServerDriver
{
	public partial class DatabaseInformationForm : Form
	{
		public DatabaseInformationForm(DbView.ConnectionInfo ci, DataRow info)
		{
			InitializeComponent();
			SetWindowTitle(String.Format("{1} on {0}", ci.Server, ci.Database));
			this.ConnectionInfo = ci;
			DoBindings(info);
		}

		private void DoBindings(DataRow info)
		{
			// Binding to read only controls.
			// This needs knowledge of the record generated by the sql
			// for now just use index numbers
			int idx = 0;
			// 0 = id
			// 1 = name (in title)
			// 2 = status
			// 3 = status desc (kind of repeats)
			idx = 3;
			bindings.Add(lblStatus, info[idx++], "Status", null); 
			bindings.Add(lblDataFileCount, info[idx++], "Data Count", null); // 4 = phyical files
			bindings.Add(lblDataFileSize, info[idx++], "Data Size", null); // 5 = size in mb
			bindings[lblDataFileSize].Converter = new ROConverter("{0} mb"); // adds units to display
			bindings.Add(lblLogfileCount, info[idx++], "Log Count", null); // 6 = Log files
			bindings.Add(lblLogFileSize, info[idx++], "Log Size", null); // 7 = size in mb
			bindings[lblLogFileSize].Converter = new ROConverter("{0} mb"); // adds units to display
			bindings.Add(lblAccess, info[idx++], "User Access", null); // 8 user access
			bindings.Add(this.lblVersion, info[idx++], "User Access", null);// 9 version/compatibility
			bindings.Add(this.lblCaseSensitive, info[idx++], "Case Sensitive", null);// 10 Case sensitive
			bindings.Add(this.lblBrokerEnabled, info[idx++], "Service Broker Enabled", null);// 11 Service broker Enabled
			bindings.Add(this.lblBrokerTrustworthy, info[idx++], "Trustworthy on", null);// 12 Service broker set trustworthy
			bindings.Add(this.lblKeyEncryted, info[idx++], "Key Encrypted", null);// 13 Service broker Key encrypted
			bindings.Add(this.lblCreated, info[idx++], "Created Date", null); // 14 Database created date skipped
			bindings.Add(this.lblbackupMode, info[idx++], "Backup Mode", null);// 15 backup - type
			bindings.Add(this.lblLastbackup, info[idx++], "Last Backup", null);// 16 backup - last backup
			bindings.Add(this.lblFullText, info[idx++], "Full Text", null);// 17 Full text
			idx += 8; // skip fields
			bindings.Add(this.lblCollation, info[idx++], "Collation", null);// 25 Collation
		}

		private void SetWindowTitle(String connection)
		{
			int pos = this.Text.IndexOf(':');
			String old = (pos == -1) ? this.Text : this.Text.Substring(0, pos);
			this.Text = String.Format("{0}: {1}", old, connection);
		}

		private void DatabaseInformationForm_Load(object sender, EventArgs e)
		{
			bindings.SetControls();
		}

		private void BtnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void BtnMoreFileInfo_Click(object sender, EventArgs e)
		{
			// execute the query into a DataSet
			String infoSql = SqlServerTemplates.DBFILEINFO_TEMPLATE;
			DataSet dsetInfo;
			try
			{
				Helper.GenericMakeDset(SqlServerDataSource.MakeConnectionString(this.ConnectionInfo), infoSql, "info", out dsetInfo);
				if (dsetInfo.Tables.Count == 0) throw new Exception("info request returned no information");
				if (dsetInfo.Tables[0].Rows.Count == 0) throw new Exception("info Table returned no rows");
			}
			catch (Exception exc)
			{
				MessageBox.Show(String.Format("Error was\n {0}", exc.Message),
											"Could not obtain information",
											MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}
			finally
			{
				this.UseWaitCursor = false;
			}
			// configure the entry form stuff
			DatabaseFileInfoForm infoForm = new DatabaseFileInfoForm(dsetInfo.Tables[0]);
			infoForm.ShowDialog(this);			// use infor
		}

		// using an entry-system object for read-only display. This is to dog-food 
		// the entry library with a view to using it in a more exciting manner later;/
		SimpleEntry.EntryBindings bindings = new SimpleEntry.EntryBindings();
		public DbView.ConnectionInfo ConnectionInfo;
	}
	/*
		Quick hack override o
	*/
	internal class ROConverter : SimpleEntry.IConverter
	{
		internal ROConverter(String template)
		{
			this.Template = template;
		}

		public String Format(object o)
		{
			return String.Format(this.Template, o);
		}

		public object Parse(String value, out String parseErrorMessage)
		{
			parseErrorMessage = "ROConverter does not support parsing from control text to object";
			return null;
		}
	
		public  string Template;
	}
}
